<?php

namespace apiend\modules\inventory\models;

use Yii;
use common\models\inventory\InventoryLog;
use common\models\inventory\InventoryBill;

/**
 * 判断批次查询结果
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class InventoryBillLogic extends \apiend\models\searchs\DashboardSearch {

    /**
     * @var array 轮播图
     */
    private $_batch = null;

    /**
     * @var array 顶级分类栏目
     */
    public $scans = 0;

    /**
     * @var \common\models\shop\ShopActivity 活动及商品列表
     */
    public $surplus = [];

    /**
     * @var \common\models\position\Position 单图推荐位推荐内容
     */
    public $loss = [];

    /**
     * @var array 聚合标签
     */
    public $abnormals = [];

    /**
     * @var array 聚合标签
     */
    public $normals = [];

    /**
     * @inheritdoc
     */
    public function setBatch($batch = null) {
        $this->_batch = $batch;
    }

    /**
     * @inheritdoc
     */
    protected function setScans() {
        return $this->_batch->amount;
    }

    /**
     * 多出的票据
     * @inheritdoc
     */
    protected function setSurplus() {
        return InventoryBill::findActive('t')
        ->joinWith([
            'logs' => function ($q) {
                $q->alias('logs')->andWhere([
                    'and',
                    ['logs.batch_id' => $this->_batch->id],
                    ['>', 'logs.status', InventoryLog::STATUS_DEFAULT],
                ])
                ->groupBy('logs.bill_id')
                ->having('count(*)>1');
            }
        ], false)
        ->all();
//        $data[] = InventoryLog::find()->where([
//            'batch_id' => $this->_batch->id
//        ])
//        ->andWhere(['>', 'status', InventoryLog::STATUS_DEFAULT])
//        ->groupBy('bill_id')->having('count(*)>1')->all();
//        return $data;
    }

    /**
     * 缺失的票据
     * @inheritdoc
     */
    protected function setLoss() {
        //select a.* from zx_bill a left join zx_inventory_log b on a.id=b.bill_id where b.id is null and a.type_id=1
        return InventoryBill::findActive('t')
        ->joinWith('logs logs')
        ->andWhere(['>', 'logs.status', InventoryLog::STATUS_DEFAULT])
        ->andWhere([
            'logs.id' => null,
            't.type_id' => $this->_batch->type_id,
        ])
        ->all();
    }

    /**
     * 异常的数据
     * @inheritdoc
     */
    protected function setAbnormals() {
        return InventoryLog::find()->where([
            'batch_id' => $this->_batch->id,
            'bill_id' => 0
        ])->all();
    }

    /**
     * 正常的数据
     * @inheritdoc
     */
    protected function setNormals() {
        return InventoryBill::findActive('t')
        ->joinWith([
            'logs' => function ($q) {
                $q->alias('logs')->andWhere([
                    'and',
                    ['logs.batch_id' => $this->_batch->id],
                    ['>', 'logs.status', InventoryLog::STATUS_DEFAULT],
                ])
                ->groupBy('logs.bill_id')
                ->having('count(*)=1');
            }
        ], false)
        ->all();
    }

}
